package org.example.dbTest.createTime

import kotlinx.coroutines.*
import java.sql.DriverManager
import java.sql.Connection
import java.sql.PreparedStatement
import kotlin.random.Random

// Doris 表结构
data class PlayerPayLog(
    val logId: Long,
    val msg: String,
    val payTime: Long,
    val orderId: String,
    val openId: String,
    val roleId: Long,
    val giftPackageId: Int,
    val castleLevel: Int,
    val paySource: Int,
    val clusterId: Int,
    val version: String,
    val serverId: Int,
    val actionType: Int,
    val sts: Double,
    val payNum: Int,
    val logName: String,
    val behId: String,
    val createTime: Long,
    val host: String,
    val roleName: String,
    val ts: Long
)

// Doris 插入服务
object DorisInserter {
    private const val URL = "jdbc:mysql://192.168.180.59:31002/mes_053"
    private const val USER = "root"
    private const val PASSWORD = "123456"
    private const val BATCH_SIZE = 10 // 每批次插入数量
    private const val TOTAL_RECORDS = 100 // 总共插入记录数
    private const val THREAD_COUNT = 10 // 协程数量

    private val insertSql = """
        INSERT INTO player_pay_log (
            logId, msg, payTime, orderId, openId, roleId, giftPackageId,
            castleLevel, paySource, clusterId, version, serverId, actionType,
            sts, payNum, logName, behId, createTime, host, roleName, ts
        ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    """.trimIndent()

    fun startInsert() = runBlocking {
        val jobs = mutableListOf<Job>()

        repeat(THREAD_COUNT) {
            jobs.add(launch {
                insertBatchData()
            })
        }

        jobs.forEach { it.join() }
        println("✅ 所有协程插入完成")
    }

    private suspend fun insertBatchData() {
        val batchCount = TOTAL_RECORDS / THREAD_COUNT / BATCH_SIZE

        for (i in 0 until batchCount) {
            val dataList = generateMockData(BATCH_SIZE)
            insertBatch(dataList)
            delay(10L) // 避免插入过快
        }
    }

    private fun generateMockData(count: Int): List<PlayerPayLog> {
        val baseTime = System.currentTimeMillis()
        val dataList = mutableListOf<PlayerPayLog>()

        for (i in 0 until count) {
            val randomOffset = Random.nextLong(-7 * 86400 * 1000, 7 * 86400 * 1000)

            val logId = 30131160042438670L + Random.nextLong(0, 100000)
            val ts = baseTime + randomOffset
            val payTime = ts
            val createTime = ts - 3600 * 1000

            dataList.add(
                PlayerPayLog(
                    logId = logId,
                    msg = "完成支付",
                    payTime = payTime,
                    orderId = "order_${logId}",
                    openId = "nnn",
                    roleId = 131073,
                    giftPackageId = 5,
                    castleLevel = 30,
                    paySource = 2,
                    clusterId = 1053,
                    version = "0.3.0",
                    serverId = 1,
                    actionType = 2671,
                    sts = 0.3,
                    payNum = 32800,
                    logName = "PlayerPaySuccessDcLog",
                    behId = "behId_${logId}",
                    createTime = createTime,
                    host = "192.168.0.155",
                    roleName = "lordY${i}",
                    ts = ts
                )
            )
        }

        return dataList
    }

    private fun insertBatch(dataList: List<PlayerPayLog>) {
        var conn: Connection? = null
        var ps: PreparedStatement? = null

        try {
            conn = DriverManager.getConnection(URL, USER, PASSWORD)
            conn.autoCommit = false
            ps = conn.prepareStatement(insertSql)

            for (data in dataList) {
                ps.setLong(1, data.logId)
                ps.setString(2, data.msg)
                ps.setLong(3, data.payTime)
                ps.setString(4, data.orderId)
                ps.setString(5, data.openId)
                ps.setLong(6, data.roleId)
                ps.setInt(7, data.giftPackageId)
                ps.setInt(8, data.castleLevel)
                ps.setInt(9, data.paySource)
                ps.setInt(10, data.clusterId)
                ps.setString(11, data.version)
                ps.setInt(12, data.serverId)
                ps.setInt(13, data.actionType)
                ps.setDouble(14, data.sts)
                ps.setInt(15, data.payNum)
                ps.setString(16, data.logName)
                ps.setString(17, data.behId)
                ps.setLong(18, data.createTime)
                ps.setString(19, data.host)
                ps.setString(20, data.roleName)
                ps.setLong(21, data.ts)

                ps.addBatch()
            }

            ps.executeBatch()
            conn.commit()
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            ps?.close()
            conn?.close()
        }
    }
}

// 主函数启动
fun main() {
    DorisInserter.startInsert()
}